import Vue from 'vue'
export default {
	namespaced: true,
	state() {
		return {
			popShow: false,
			popData:null, //存放弹窗数据
			editIdx:-1, //标志购物车编辑
			cartList: [{
				name: '甜甜梅梅',
				french: 'Gâteau aux fraises et chocolat',
				img: "https://static.mcake.com/new_goods/tiantianmeimei/R9002/list/1.jpg",
				num:1, //商品数据
				dropIdx:0, //标志选中的规格序号
				id:"15722",
				ischeck:true,
				list: [{
					fittings: "5套餐具",
					bcid: 1,
					pprice: "218.00",
					size: "14cm*8cm(直径*高）",
					sku: "R9001",
					spec:"1磅",
					bcname: "蛋糕",
					id:15722
				}, {
					fittings: "5套餐具",
					bcid: 1,
					pprice: "218.00",
					size: "14cm*8cm(直径*高）",
					sku: "R9001",
					bcname: "蛋糕",
					id:15723
				}]
			}],
		}
	},
	getters:{
		allInfo(state){
			let allCheck=true
			let allPrice=0
			state.cartList.forEach(item=>{
				let {num,dropIdx,list} = item
				if(item.ischeck){
					allPrice+=num*list[dropIdx].pprice
				}
				if(!item.ischeck){
					allCheck=false
				}
			})
			return {
				allCheck,allPrice
			}
		}
	},
	mutations: {
		popShowMut(state, bool = true) { //控制弹窗显示
			state.popShow = bool
		},
		popDataMut(state,goodsObj){ //为弹窗设置数据
			state.popData = goodsObj
		},
		editIdxMut(state,idx){ 
			state.editIdx = idx
		},
		cartAddMut(state,{goodsObj}){ //加入购物车
			let {id,dropIdx} = goodsObj
			let {cartList,editIdx} = state
			let len = cartList.length
			if(editIdx!=-1){ //编辑
				cartList[editIdx] = goodsObj
				state.cartList = [...cartList]
				return
			}
			for(let i=0;i<len;i++){
				if(cartList[i].id==id&&cartList[i].dropIdx==dropIdx){
					state.cartList[i].num+=goodsObj.num //新增同类商品
					return
				}
			}
			Vue.set(goodsObj,'ischeck',true)
			state.cartList.push(goodsObj) //新增不同商品
			// state.popShow = false
		},
		cartCheckMut(state,idx){ //单选
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		cartAllCheckMut(state,allCheck){ //全选
			state.cartList.forEach(item=>{
				item.ischeck = !allCheck
			})
		}
	}
}
